from Common.BaseModel import db, BaseModel


class ModuleModel(BaseModel):
    __tablename__ = 'sys_module'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    pid = db.Column(db.Integer, db.ForeignKey('sys_module.id', ondelete='RESTRICT', onupdate='CASCADE'))
    name = db.Column(db.String(64), nullable=False)
    type = db.Column(db.String(64), nullable=False)
    icon = db.Column(db.String(64))
    url = db.Column(db.String(128))
    desc = db.Column(db.Text)

    children = db.relationship('ModuleModel', backref=db.backref('parent', remote_side=[id]))

    def __init__(self, dataDict):
        if not isinstance(dataDict, dict):
            return
        self.pid = dataDict['pid']
        self.name = dataDict['name']
        self.type = dataDict['type']
        self.icon = dataDict['icon']
        self.url = dataDict['url']
        self.desc = dataDict['desc']


class ActionModel(BaseModel):
    __tablename__ = 'sys_action'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    module_id = db.Column(db.Integer, db.ForeignKey('sys_module.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
    name = db.Column(db.String(64), nullable=False)
    mark = db.Column(db.String(64), nullable=False, unique=True)
    api = db.Column(db.String(64), nullable=False)
    method = db.Column(db.String(32), nullable=False)
    scope = db.Column(db.String(32))

    module = db.relationship('ModuleModel', backref='actions')

    def __init__(self, dataDict):
        if not isinstance(dataDict, dict):
            return
        self.module_id = dataDict['module_id']
        self.name = dataDict['name']
        self.mark = dataDict['mark']
        self.api = dataDict['api']
        self.method = dataDict['method']
        self.scope = dataDict['scope']
